package com.heima;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

class RectangleShape implements DrawableShape {
    double x, y, width = 100, height = 50;
    Color color;
    String text;

    RectangleShape(double x, double y, Color color, String text) {
        this.x = x;
        this.y = y;
        this.color = color;
        this.text = text;
    }

    @Override
    public void draw(GraphicsContext gc) {
        gc.setFill(color);
        gc.fillRect(x, y, width, height);
        // 设置固定边框粗细为1.0，避免受连接线笔触影响
        gc.setLineWidth(1.0);
        gc.setStroke(color);
        gc.strokeRect(x, y, width, height);
        gc.setFill(Color.BLACK);
        gc.fillText(text, x + width/2, y + height/2);
    }

    @Override
    public boolean contains(double mouseX, double mouseY) {
        return mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height;
    }

    @Override
    public void setColor(Color color) {
        this.color = color;
    }

    @Override
    public void setText(String text) {
        this.text = text;
    }

    @Override
    public Color getColor() {
        return color;
    }

    @Override
    public String getText() {
        return text;
    }

    @Override
    public void move(double deltaX, double deltaY) {
        x += deltaX;
        y += deltaY;
    }

    @Override
    public DrawableShape copy() {
        return new RectangleShape(x, y, color, text);
    }
    
    @Override
    public Element toXml(Document doc) {
        Element element = doc.createElement("shape");
        element.setAttribute("type", "rectangle");
        element.setAttribute("x", String.valueOf(x));
        element.setAttribute("y", String.valueOf(y));
        element.setAttribute("width", String.valueOf(width));
        element.setAttribute("height", String.valueOf(height));
        element.setAttribute("color", color.toString());
        element.setAttribute("text", text);
        
        return element;
    }
    
    // 新增方法：绘制连接点
    @Override
    public void drawConnectionPoints(GraphicsContext gc) {
        // 绘制矩形图形的四个连接点（上下左右）
        double halfWidth = width / 2;
        double halfHeight = height / 2;
        
        // 设置连接点为蓝色小方块
        gc.setFill(Color.BLUE);
        
        // 上部连接点
        gc.fillRect(x + halfWidth - 5, y - 5, 10, 10);
        // 下部连接点
        gc.fillRect(x + halfWidth - 5, y + height - 5, 10, 10);
        // 左侧连接点
        gc.fillRect(x - 5, y + halfHeight - 5, 10, 10);
        // 右侧连接点
        gc.fillRect(x + width - 5, y + halfHeight - 5, 10, 10);
    }
    
    // 新增方法：获取选中的连接点
    @Override
    public ConnectionPoint getSelectedConnectionPoint(double mouseX, double mouseY) {
        // 检查鼠标是否点击在某个连接点上
        double halfWidth = width / 2;
        double halfHeight = height / 2;
        
        // 定义四个连接点的位置
        double[][] points = {
            {x + halfWidth, y},             // 上
            {x + halfWidth, y + height},     // 下
            {x, y + halfHeight},              // 左
            {x + width, y + halfHeight}       // 右
        };
        
        // 判断鼠标位置是否在某个连接点区域内（10x10像素）
        for (int i = 0; i < points.length; i++) {
            if (Math.abs(mouseX - points[i][0]) <= 10 && Math.abs(mouseY - points[i][1]) <= 10) {
                String direction;
                switch (i) {
                    case 0: direction = "top"; break;
                    case 1: direction = "bottom"; break;
                    case 2: direction = "left"; break;
                    default: direction = "right";
                }
                
                // 计算连接点坐标
                double pointX, pointY;
                
                switch (direction) {
                    case "top":
                        pointX = x + halfWidth;
                        pointY = y;
                        break;
                    case "bottom":
                        pointX = x + halfWidth;
                        pointY = y + height;
                        break;
                    case "left":
                        pointX = x;
                        pointY = y + halfHeight;
                        break;
                    case "right":
                        pointX = x + width;
                        pointY = y + halfHeight;
                        break;
                    default:
                        return null;
                }
                
                // 使用正确参数调用 ConnectionPoint 构造方法
                return new ConnectionPoint(pointX, pointY, direction, this);
            }
        }
        
        return null;
    }
}
